package ru.nsu.fit.client.core.soap.send_answer;

import org.ksoap2.serialization.SoapObject;
import ru.nsu.fit.client.core.soap.SOAPExecutor;

import java.util.AbstractMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class SendAnswer {
    private static final String WSDL_NAME = "send_answer";
    private static final String METHOD_NAME = "sendAnswer";
    private static final String NAMESPACE = "http://send_answer.ws.fit.nsu.ru/";

    public static SendAnswerOutput execute(final String serverLocation, long userSessionId, int questionId, int answerId) throws Exception
    {
        String wsdlLocation = serverLocation + WSDL_NAME + "?wsdl";

        Set<Map.Entry<String, String>> params = new HashSet<Map.Entry<String,String>>();
        params.add(new AbstractMap.SimpleEntry<String, String>("userSessionId", String.valueOf(userSessionId)));
        params.add(new AbstractMap.SimpleEntry<String, String>("questionId", String.valueOf(questionId)));
        params.add(new AbstractMap.SimpleEntry<String, String>("answerId", String.valueOf(answerId)));

        SoapObject response = SOAPExecutor.executeSOAP(wsdlLocation, NAMESPACE, METHOD_NAME, params);

        SendAnswerOutput out = new SendAnswerOutput();
        out.setSuccessful(Boolean.parseBoolean(response.getPropertyAsString("successful")));
        if (response.hasProperty("errorMessage"))
        {
            out.setErrorMessage(response.getPropertyAsString("errorMessage"));
        }
        if (response.hasProperty("errorCode"))
        {
            out.setErrorCode(Integer.parseInt(response.getPropertyAsString("errorCode")));
        }

        return out;
    }
}
